<template>
  <div class="page-container">
    <ex_super_table_page v-bind="option" ref="SuperTablePage" @selection-change="e => selection = e">
    </ex_super_table_page>
  </div>
</template>

<script>
import superTablePageFunction from '@/mixin/super-table-page-function';
import emitOnFunction from '@/mixin/emit-on-function';
let emitKey = 'overseas=>wms_count_order_result'
export default {
  name: "wms_count_order_result",
  title: "title_count_result",
  mixins: [superTablePageFunction, emitOnFunction(emitKey)],
  data() {
    return {
      selection: [],
      option: {
        search: {
          model: [

            { label: "label_stocktaking_order_code", key: "orderNo", type: 'input', },
            {
              label: "label_status", key: "status", type: 'select',
              enum: 'CountOrderResultStatusEnum',
            },
            {
              label: "label_srm_api_order_type", key: "type", type: 'select',
              enum: 'CountOrderTypeEnum',
            },
            {
              label: "label_inventory_method", key: "mode", type: 'select',
              enum: 'CountOrderModeEnum',
            },
            { label: "label_create_user", key: "creator", type: 'input', },
            {
              label: "label_start_time",
              key: "createDateStart",
              fold: true,
              type: 'date-picker',
              attrs: { type: "datetime" },
            },
            {
              label: "label_end_time",
              key: "createDateEnd",
              fold: true,
              type: 'date-picker',
              attrs: { type: "datetime" },
            },
          ],
        },
        actionExtra: [
          { label: 'label_replay_stocktaking_operate', attrs: { type: "primary" }, auth: 'wms_count_order_result_replay', click: this.repeat },
          {
            label: 'label_result_confirm', attrs: { type: "primary" }, auth: "wms_count_order_result_confirm", click: () => {

              this.checkResult(this.selection)
            }
          },
          { label: 'label_result_upload', attrs: { type: "primary" }, auth: "wms_count_order_result_upload", click: this.uploadOmsStock },
        ],
        action: [
        ],

        table: {
          url: "/wms-api/inside/CountOrderRpc/selectResultPage.json",
          attrs: {},
          model: [
            { type: 'selection', clickToggle: true, },
            { key: 'orderNo', label: 'label_stocktaking_order_code', 'min-width': 150, },
            { key: 'bizType', label: 'label_business_type', 'width': 120, formatter: (r, c, v) => this.$enumShow(v, "BizTypeEnum") },
            { key: 'status', label: 'label_status', 'width': 140, formatter: (r, c, v, i) => this.$enumShow(v, "CountOrderStatusEnum"), },
            { key: 'type', label: 'label_stocktaking_type', 'width': 120, formatter: (r, c, v, i) => this.$enumShow(v, "CountOrderTypeEnum"), },
            { key: 'mode', label: 'label_stocktaking_way', 'width': 120, formatter: (r, c, v, i) => this.$enumShow(v, "CountOrderModeEnum"), },
            { key: 'isBlind', label: 'label_blind_check', 'width': 120, formatter: (r, c, v, i) => this.$enumShow(v, "YesOrNoEnum"), },

            { key: 'totalStockNum', label: 'label_plan_stocktaking_stock_count', 'width': 100, },
            { key: 'actualCountNum', label: 'label_actual_stocktaking_stock_count', 'width': 100, },
            { key: 'isDiff', label: 'label_is_diff', 'width': 100, formatter: (r, c, v, i) => this.$enumShow(v, "YesOrNoEnum"), },
            { key: 'diffNum', label: 'label_diff_count', 'width': 100, },
            { key: 'isUploadClient', label: 'label_result_is_upload', 'width': 100, formatter: (r, c, v, i) => this.$enumShow(v, "YesOrNoEnum"), },

            { key: 'creator', label: 'label_create_user', 'width': 120, },
            { key: 'gmtCreate', label: 'label_create_time', 'width': 130, 'show-overflow-tooltip': true, formatter: (r, c, v, i) => this.$dateFormat(v) },
            {
              type: 'operate', width: 210, fixed: 'right', operate: [
                { label: 'label_view_detail', click: (row) => this.clickDetail(row) },
                {
                  label: 'label_replay_stocktaking_operate', auth: 'wms_count_order_result_replay',
                  disabled: (row) => row.status == 'COMPLETE' || row.status == 'WAIT_REVIEW' || row.status == 'CANCELLED' || row.status == 'REPEAT_COMPLETE',
                  click: (row) => this.clickOperate(row)
                },
                {
                  label: 'label_result_confirm',
                  disabled: (row) => ![ 'WAIT_REVIEW','REPEAT_COMPLETE'].includes(row.status),
                  click: (row) => this.checkResult([row])
                }
              ]
            },
          ],

        }
        ,

      },
    };
  },
  methods: {
    clickOperate(row) {
      this.$router.push({ path: '/page/wms/inside/count_operate', query: { id: row.id, code: row.orderNo, type: row.type, isBlind: row.isBlind, emitKey } })
    },
    repeat() {
      let orderIds = []
      let _list = this.selection

      if (_list.length == 0) {
        return this.$message.error(this.$tt('tip_please_select_order'))
      }

      for (var i = 0; i < _list.length; i++) {
        orderIds.push(_list[i].id)
      }
      this.$ajax({
        url: "/wms-api/inside/CountOrderRpc/confirmRepeatCount.json",
        type: 'json',
        method: 'post',
        data: {
          orderIds
        },
        success: (res) => {
          this.$message.success(this.$tt('tip_submit_success'))
          this.reload()
        },
      })
    },
    checkResult(list) {
      if (_.isEmpty(list)) {
        return this.$message.error(this.$tt('tip_please_select_order'))
      }
      if (list.some(row=>![ 'WAIT_REVIEW','REPEAT_COMPLETE'].includes(row.status))) {
        return this.$message.error(this.$tt('tip_part_order_status_error_no_operate'))
      }
      this.$confirm(this.$tt('text_confirm_sure_operate_count_record', { count: list.length }), this.$t("label_tip"), {
        type: 'warning'
      }).then(() => {
        this.$ajax({
          url: "/wms-api/inside/CountOrderRpc/confirmCountResult.json",
          type: 'json',
          method: 'post',
          data: {
            orderIds: list.map(it => it.id)
          },
          success: (res) => {
            this.$message.success(this.$tt('tip_submit_success'))
            this.reload()
          },
        })
      }).catch(() => {

      });

    },
    uploadOmsStock() {
      let orderIds = []
      let _list = this.selection

      if (_list.length === 0) {
        return this.$message.error(this.$tt('tip_please_select_order'))
      }

      for (var i = 0; i < _list.length; i++) {
        if (_list[i].status != 'COMPLETE') {
          return this.$message.error("请选择已完成的盘点单")
        }
        orderIds.push(_list[i].id)
      }
      this.$ajax({
        url: "/wms-api/inside/CountOrderRpc/uploadCountResult.json",
        type: 'json',
        method: 'post',
        data: {
          orderIds
        },
        success: (res) => {
          this.$message.success(this.$tt('tip_submit_success'))
          this.reload()
        },
      })
    },
    clickDetail(row) {
      this.$router.push({ path: '/page/wms/inside/count_detail', query: { id: row.id, emitKey } })
    }
  }
}
</script>

<style scoped></style>
